/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package json;

import engine.Player;
import engine.Singleton;
import engine.Soluter;
import engine.randomizer;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import server.ServeOneThread;
import server.Server;
import org.apache.log4j.Logger;

/**
 *
 * @author Alexx
 */
public class Protocol {
  Server mysrv;
  private static final Logger log = Logger.getLogger(Protocol.class);
  
  
  
  public String get(String str, Server mysrv) throws org.json.simple.parser.ParseException
   {
       String type;
       String rety = str;
       this.mysrv = mysrv;
       IController control = new Controller(mysrv);
       log.info("Start Protocol");
       
//       catch (Exception e) {
//            log.fatal("Game exception", e);
//        }
       
       log.debug("Protocol str" + str);
       JsonSerializer json = new JsonSerializer();
       json.startGet(str);
       
       type = json.getString("Type");
       
       if (type.equals("Auth"))
       {
         String name = json.getString("Name");
         log.debug(name);
         rety = "Autorization";
         try {
           control.execute(rety, name, null, null, null);
         } catch (Exception e)
        {log.info("test" + e);}
         
       }
       
       if (type.equals("SetPartner"))
       {
         String name = json.getString("Name");
         String offerName = json.getString("OfferName");
         log.debug(name + " " + offerName);
         rety = "Partner";
         try {
         control.execute(rety, name, offerName, null, null);
         } catch (Exception e)
        {log.info("test");}
       }
       
       if (type.equals("Refuse"))
       {
         String name = json.getString("Name");
         log.debug(name);
         rety = "Refuse";
          try {
         control.execute(rety, name, null, null, null);
         } catch (Exception e)
        {log.info("test");}
       }
       
       if (type.equals("StartGame"))
       {
         String player1 = json.getString("YouLogin");
         String player2 = json.getString("PartnerLogin");
         log.debug(player1 + " " + player2);
         rety = "StartGame";
         try {
         control.execute(rety, player1, player2, null, null);
         } catch (Exception e)
        {log.info("test");}
       }
       
       if (type.equals("Step"))
       {
         String row = json.getString("Row"); 
         String col = json.getString("Col"); 
         String name = json.getString("Name");
         log.debug(row + " " + col);
         rety = "Step";
         try {
         control.execute(rety, name, null, row, col);
         } catch (Exception e)
        {log.info("test");}
       }
       
       if (type.equals("End"))
       {
         log.debug("End");
         rety = "End";
         try {
         control.execute(rety, null, null, null, null);
         } catch (Exception e)
        {log.info("test");}
       }
       
       if (type.equals("Wait"))
       {
         log.debug("Wait");
         rety = "Wait";
         try {
         control.execute(rety, null, null, null, null);
         } catch (Exception e)
        {log.info("test");}
       }

       
       if (type.equals("Refresh"))
       {
         log.debug("Refresh");
         rety = "Refresh";
         try {
         control.execute(rety, null, null, null, null);
         } catch (Exception e)
        {log.info("test");}
       }
       
       return rety;
   }

}
